package com.moodswings.server;

import com.moodswings.server.model.Mood;
import com.moodswings.server.service.MoodService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Prihvata zahtev i u browseru ispisuje poslednjih 25 raspolozenja unetih u tabelu
 */
public class IndexServlet extends HttpServlet {

	MoodService moodService;

	public IndexServlet() {
		moodService = new MoodService();
	}

	/**
	 * Prihvatanje zahteva i vracanje putem Dispatcher-a. U dispatcheru je html prikaz sa kodom koji prikazuje
	 * unose iz baze
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Mood> moodList = moodService.findLastMoods(25);

		req.setAttribute("moodList", moodList);

		RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
		requestDispatcher.forward(req, resp);

	}

}
